package com.coders.android.taxi;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.coders.android.R;

public class SignInActivity extends Activity {

	private EditText signInId;
	private EditText signInName;
	private EditText signInFirstName;
	private EditText signInLastName;
	private EditText signInEmail;
	
	private Button signInButton;
	
	private String IMEI;
	Register register;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sign_in);
		
		initComponents();
	}
	
	private void initComponents() {
		
		signInId = (EditText) findViewById( R.id.activity_sign_in_id );
		signInName = (EditText) findViewById( R.id.activity_sign_in_name );
		signInFirstName = (EditText) findViewById( R.id.activity_sign_in_firstname );
		signInLastName = (EditText) findViewById( R.id.activity_sign_in_lastname );
		signInEmail = (EditText) findViewById( R.id.activity_sign_in_email );
		
		signInButton = (Button) findViewById( R.id.activity_sign_in_button );
		signInButton.setOnClickListener( new SignIn() );
		
		TelephonyManager telephonyManager = (TelephonyManager) getSystemService( Context.TELEPHONY_SERVICE );
		IMEI = telephonyManager.getDeviceId();
	}
	
	class SignIn implements OnClickListener {

		@Override
		public void onClick(View v) {

			if( signInId.getText().toString().equals( "" ) ) {
				Toast.makeText( SignInActivity.this, "Ingrese su identificacion", Toast.LENGTH_SHORT ).show();
				return;
		    } else if( signInName.getText().toString().equals( "" ) ) {
				Toast.makeText( SignInActivity.this, "Ingrese su nombre", Toast.LENGTH_SHORT ).show();
				return;
			} else if( signInFirstName.getText().toString().equals( "" ) ) {
				Toast.makeText( SignInActivity.this, "Ingrese su primer apellido", Toast.LENGTH_SHORT ).show();
				return;
			} else if( signInLastName.getText().toString().equals( "" ) ) {
				Toast.makeText( SignInActivity.this, "Ingrese su segundo apellido", Toast.LENGTH_SHORT ).show();
				return;
			} else if( signInEmail.getText().toString().equals( "" ) ) {
				Toast.makeText( SignInActivity.this, "Ingrese su correo electronico", Toast.LENGTH_SHORT ).show();
				return;
			}
			
			register = new Register();
			register.execute( 
					signInId.getText().toString(),
					signInName.getText().toString(),
					signInFirstName.getText().toString(),
					signInLastName.getText().toString(),
					signInEmail.getText().toString()
					);
		}}
	
	class Register extends AsyncTask<String, Integer, Boolean> {

		private final String TAG = this.getClass().getSimpleName();
		private final String IP_PORT = "201.232.166.244:8080";
		/* registrar/{imei}/{cedula}/{nombres}/{primerApellido}/{segundoApellido}/{correo} */
		private final String URL = "/AndroidService/rest/android/login/registrar/";
		
		@Override
		protected Boolean doInBackground(String... params) {
			
			HttpClient httpClient = null;
			HttpGet httpGet = null;
			/*HttpPost httpPost = null;*/
			HttpResponse httpResponse = null;
			
			try {
				httpClient = new DefaultHttpClient();
				httpGet = new HttpGet( IP_PORT + URL + IMEI + "/" + params[0] + "/" + params[1] + "/" + params[2] + "/" + params[3] + "/" + params[4] );
				httpGet.setHeader( "content-type", "application/json" );
				/*httpPost = new HttpPost( IP_PORT + URL + IMEI + "/" + params[0] + "/" + params[1] + "/" + params[2] + "/" + params[3] + "/" + params[4] );
				httpPost.setHeader( "content-type", "application/json" );
				
				 construimos el objeto cliente en formato JSON 
				JSONObject jsonObject = new JSONObject();
				jsonObject.put( "imei", IMEI );
				jsonObject.put( "cedula", params[0] );
				jsonObject.put( "nombres", params[1] );
				jsonObject.put( "primerApellido", params[2] );
				jsonObject.put( "segundoApellido", params[3] );
				jsonObject.put( "correo", params[4] );
				
				StringEntity stringEntity = new StringEntity( jsonObject.toString() );
				
				httpPost.setEntity( stringEntity );
				
				httpResponse = httpClient.execute( httpPost );*/
				httpResponse = httpClient.execute( httpGet );
								
				String response = EntityUtils.toString( httpResponse.getEntity() );
				if( response.equals( "FAIL" ) )
					return false;
				
				Log.i( TAG, "OK" );
				
			} catch (Exception e) {
				Log.e( TAG, "FAIL", e );
	        	return false;
			}
			return true;
		}
		
		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			
			if( result )
				Toast.makeText( SignInActivity.this, "Registro exitoso!\nLos datos han sido enviados a su email.", Toast.LENGTH_LONG ).show();
		}		
	}	
}
